{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="col-md-8">
        <h1 class="mb-4">最新文章</h1>
        {% if posts.items %}
            {% for post in posts.items %}
            <div class="card mb-4 post-card">
                <div class="card-body">
                    <!-- 可点击的标题 -->
                    <h5 class="card-title post-title">
                        <a href="{{ url_for('post_detail', post_id=post.id) }}" class="text-decoration-none text-dark">
                            {{ post.title }}
                        </a>
                    </h5>

                    <!-- 文章预览，可点击查看全文 -->
                    <div class="card-text mb-3 post-preview">
                        <a href="{{ url_for('post_detail', post_id=post.id) }}" class="text-decoration-none text-muted">
                            {{ post.content[:150] }}{% if post.content|length > 150 %}...
                            <span class="read-more">[阅读全文]</span>
                            {% endif %}
                        </a>
                    </div>

                    <!-- 文章元信息和操作按钮 -->
                    <div class="d-flex justify-content-between align-items-center post-meta">
                        <div class="text-muted">
                            <small>
                                <a href="{{ url_for('user_profile', username=post.author.username) }}"
                                   class="text-decoration-none text-muted">
                                    作者: {{ post.author.username }}
                                </a>
                            </small>
                            <small class="ms-3">发布时间: {{ post.date_posted.strftime('%Y-%m-%d %H:%M') }}</small>
                            <small class="ms-3">评论: {{ post.comments|length }}</small>
                        </div>

                        {% if current_user.is_authenticated and current_user == post.author %}
                        <div class="btn-group btn-group-sm">
                            <a href="{{ url_for('update_post', post_id=post.id) }}" class="btn btn-outline-primary">
                                编辑
                            </a>
                            <a href="{{ url_for('delete_post', post_id=post.id) }}" class="btn btn-outline-danger"
                               onclick="return confirm('确定要删除这篇文章吗？')">
                                删除
                            </a>
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
            {% endfor %}

            <!-- 分页导航 -->
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    {% if posts.has_prev %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('index', page=posts.prev_num) }}">上一页</a>
                    </li>
                    {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">上一页</span>
                    </li>
                    {% endif %}

                    {% for page_num in posts.iter_pages(left_edge=1, right_edge=1, left_current=1, right_current=2) %}
                        {% if page_num %}
                            {% if page_num == posts.page %}
                            <li class="page-item active">
                                <span class="page-link">{{ page_num }}</span>
                            </li>
                            {% else %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('index', page=page_num) }}">{{ page_num }}</a>
                            </li>
                            {% endif %}
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                        {% endif %}
                    {% endfor %}

                    {% if posts.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('index', page=posts.next_num) }}">下一页</a>
                    </li>
                    {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">下一页</span>
                    </li>
                    {% endif %}
                </ul>
            </nav>
        {% else %}
            <div class="alert alert-info text-center">
                <h4>还没有任何文章</h4>
                <p class="mb-0">快来<a href="{{ url_for('new_post') }}" class="alert-link">发布第一篇</a>吧！</p>
            </div>
        {% endif %}
    </div>

    <!-- 侧边栏 -->
    <div class="col-md-4">
        <div class="card sidebar-card">
            <div class="card-header bg-primary text-white">
                <h5 class="mb-0">博客统计</h5>
            </div>
            <div class="card-body">
                <div class="text-center mb-3">
                    <h3 class="text-primary">{{ posts.total if posts else 0 }}</h3>
                    <p class="text-muted">总文章数</p>
                </div>
                {% if current_user.is_authenticated %}
                <a href="{{ url_for('new_post') }}" class="btn btn-primary w-100">
                    写新文章
                </a>
                {% else %}
                <div class="text-center">
                    <p class="text-muted">登录后开始写作</p>
                    <a href="{{ url_for('login') }}" class="btn btn-outline-primary me-2">登录</a>
                    <a href="{{ url_for('register') }}" class="btn btn-primary">注册</a>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- 快速操作卡片 -->
        {% if current_user.is_authenticated %}
        <div class="card mt-4">
            <div class="card-header">
                <h6 class="mb-0">快速操作</h6>
            </div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <a href="{{ url_for('new_post') }}" class="btn btn-outline-primary btn-sm">
                        写文章
                    </a>
                    <a href="{{ url_for('index') }}" class="btn btn-outline-secondary btn-sm">
                        返回首页
                    </a>
                </div>
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}